package com.nvshen.zhangmeiying.utils;

import android.content.Context;
import android.graphics.Bitmap;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Random;

/**
 * Created by wxc on 14-8-20.
 */
public class DataUtil {
    public static final String PATH_IMAGES = Utils.getCacheRootPath()+"images/";

    public static String getStoredAudioFolder()
    {
        return Utils.getCacheRootPath()+"audio/";
    }

    public static String getStoredImageFolder()
    {
        return PATH_IMAGES;
    }

    public static File getStoredImageFileFolder()
    {
        File f = new File(PATH_IMAGES);
        return f;
    }

    public static String getAvatarFileName()
    {
        int ran = new Random().nextInt(1000);
        return "avatar_"+(System.currentTimeMillis()/1000)+"_"+ran+".jpg";
    }

    public static String getImageFileName()
    {
        int ran = new Random().nextInt(1000);
        return "image_"+(System.currentTimeMillis()/1000)+"_"+ran+".jpg";
    }

    public static String getAudioFileName()
    {
        int ran = new Random().nextInt(1000);
        return "audio_"+(System.currentTimeMillis()/1000)+"_"+ran+".mp3";
    }

    public static File createFile(String folderName,String fileName){
        File folder = new File(folderName);
        if (folder.exists() == false)
        {
            folder.mkdirs();
        }

        File f = new File(folder, fileName);
        try
        {
            f.createNewFile();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return f;
    }

    /**
     * 保存Bitmap
     *
     * @param context
     * @param folderName
     * @param fileName
     * @param mBitmap
     * @return 保存的路径
     */
    public static String saveMyBitmap(Context context, String folderName, String fileName, Bitmap mBitmap)
    {
        File folder = new File(folderName);
        if (folder.exists() == false)
        {
            folder.mkdirs();
        }

        File f = new File(folder, fileName);
        try
        {
            f.createNewFile();
            FileOutputStream fOut = null;
            fOut = new FileOutputStream(f);
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            fOut.flush();
            fOut.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        if (folderName.endsWith("/"))
        {
            return folderName + fileName;
        }
        else
        {
            return folderName + "/" + fileName;
        }
    }

    public static String getStoredImagePath(int uid)
    {
        Calendar calendar = Calendar.getInstance();
        return PATH_IMAGES + uid + "/" + calendar.getTimeInMillis() + ".jpg";
    }

    public static String getStoredImageFileName(String login)
    {
        Calendar calendar = Calendar.getInstance();
        return new StringBuilder(login).append(calendar.getTimeInMillis()).append(".jpg").toString();
    }
}
